package flc.ast.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.model.ColorTuneRange;
import com.stark.imgedit.utils.BmpUtil;

import flc.ast.R;
import flc.ast.databinding.FragmentAdjustBinding;
import stark.common.basic.base.BaseNoModelFragment;

public class AdjustFragment extends BaseEditFragment<FragmentAdjustBinding> implements SeekBar.OnSeekBarChangeListener {

    private float mBrightness = 1;
    private float mSaturation = 1;
    private float mContrast = 0;
    private Bitmap mRetNoFilterBitmap;
    private Bitmap mEditFilterBitmap;

    public static AdjustFragment newInstance() {
        AdjustFragment fragment = new AdjustFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mRetNoFilterBitmap = null;
        mBrightness = 1;
        mSaturation = 1;
        mContrast = 0;
    }

    @Override
    public void backToMain() {
        mSingleResultActivity.mode = ImgEditActivity.MODE_NONE;
        mSingleResultActivity.mImgView.setVisibility(View.VISIBLE);
        mSingleResultActivity.mImgView.setImageBitmap(mSingleResultActivity.getMainBit());
        mSingleResultActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_adjust;
    }

    @Override
    protected void initView() {
        mSingleResultActivity.mode = ImgEditActivity.MODE_COLOR_TUNE;

        mDataBinding.sbContrast.setOnSeekBarChangeListener(this);
        mDataBinding.sbBrightness.setOnSeekBarChangeListener(this);
        mDataBinding.sbBrightness.setProgress(calSeekBarProgress(mDataBinding.sbBrightness,
                mBrightness, ColorTuneRange.BRIGHTNESS));
        mDataBinding.sbContrast.setProgress(calSeekBarProgress(mDataBinding.sbContrast,
                mContrast, ColorTuneRange.CONTRAST));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sbBrightness:
                mBrightness = calSeekBarRealValue(mDataBinding.sbBrightness, progress, ColorTuneRange.BRIGHTNESS);
                updateImageViewBitmap();
                break;
            case R.id.sbContrast:
                mContrast = calSeekBarRealValue(mDataBinding.sbContrast, progress, ColorTuneRange.CONTRAST);
                updateImageViewBitmap();
                break;
            default:
                break;
        }
    }

    public void applyColorTune() {
        if (mRetNoFilterBitmap == null) {
            backToMain();
            return;
        }
        mSingleResultActivity.changeMainBitmap(mRetNoFilterBitmap, mEditFilterBitmap, true);
        backToMain();
    }

    private void updateImageViewBitmap() {
        Bitmap preRetBitmap = mRetNoFilterBitmap;
        mRetNoFilterBitmap = BmpUtil.adjust(mSingleResultActivity.getmNoFilterEditBitmap(), mBrightness, mSaturation, mContrast);
        mEditFilterBitmap = BmpUtil.adjust(mSingleResultActivity.getmHasFilterBitmap(), mBrightness, mSaturation, mContrast);
        if (mRetNoFilterBitmap != null && mEditFilterBitmap != null) {
            if (mSingleResultActivity.mHasOriginFilter) {
                mSingleResultActivity.mImgView.setImageBitmap(mRetNoFilterBitmap);
            } else {
                mSingleResultActivity.mImgView.setImageBitmap(mEditFilterBitmap);
            }
        }
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress, ColorTuneRange range) {
        int progressRange = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            progressRange = seekBar.getMax() - seekBar.getMin();
        }
        float value = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            value = (progress - seekBar.getMin()) * 1.0f / progressRange * range.getRange() + range.getMin();
        }
        return value;
    }

    private int calSeekBarProgress(SeekBar seekBar, float value, ColorTuneRange range) {
        float valuePercent = (value - range.getMin()) / range.getRange();
        int progress = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            progress = (int) (seekBar.getMin() + (seekBar.getMax() - seekBar.getMin()) * valuePercent);
        }
        return progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}